import {Component, EventEmitter, OnInit, Output} from '@angular/core';

@Component({
  selector: 'app-price-quect',
  templateUrl: './price-quect.component.html',
  styleUrls: ['./price-quect.component.css']
})
export class PriceQuectComponent implements OnInit {

  stockCode:string = "IBM";
  price:number;

  // 发射事件
  @Output()
  lastPrice:EventEmitter<PriceQuect> = new EventEmitter();

  // @Output(productPrice)
  // lastPrice:EventEmitter<PriceQuect> = new EventEmitter();

  // 发射事件
  @Output()
  buy:EventEmitter<PriceQuect> = new EventEmitter();

  constructor() {
    setInterval(() => {
      let priceQuote:PriceQuect = new PriceQuect(this.stockCode, 100*Math.random());
      this.price = priceQuote.lastPrice;
      this.lastPrice.emit(priceQuote);
    },1000)
  }

  ngOnInit() {
  }

  buyStock(even) {
    this.buy.emit(new PriceQuect(this.stockCode,this.price));
  }

}

export class PriceQuect {
  constructor(public stockCode:string,
              public lastPrice:number
  ){

  }
}
